package com.xrui.hbaseview;


import com.xrui.hbase.tools.BaseTool;
import com.xrui.hbase.tools.HBaseToolLauncher;
import com.xrui.hbaseview.ui.forms.Window;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * GUI tool for viewing table data.
 * <p>
 * <h2>Examples:</h2>
 * <pre>
 *   hbase viewer
 * </pre>
 * </pre>
 */
public final class ViewTool extends BaseTool {
    private static final Logger LOG = LoggerFactory.getLogger(ViewTool.class);

    /**
     * Program entry point.
     *
     * @param args The command-line arguments.
     * @throws Exception If there is an error.
     */
    public static void main(String[] args) throws Exception {
        HBaseToolLauncher hBaseToolLauncher = new HBaseToolLauncher();
        int ret = hBaseToolLauncher.run(new ViewTool(), args);
        System.exit(ret);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getName() {
        return "viewer";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getDescription() {
        return "View hbase table data.";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getCategory() {
        return "Data";
    }

    /**
     * Implements the --do=dump operation.
     *
     * @throws Exception on error.
     */
    private void runHViewer() throws Exception {
        Thread thread = new Thread(
            () -> {
                try {
                    Window.main(null);
                }catch (Exception e){
                    LOG.error("Exception occurred", e);
                    getPrintStream().printf("ERROR: " + e.getMessage());
                }
            });
        thread.start();
        while (!thread.isInterrupted()) {
            Thread.sleep(100);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected int run(List<String> nonFlagArgs) throws Exception {
        runHViewer();
        return SUCCESS;
    }
}
